package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.font.FontLibrary;
import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.toolkit.GUIToolkit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

public class ScratchPanel extends JPanel {

	private final JPanel panel = new JPanel();
	private final JButton btnNewButton = new JButton("ض");
	private final JButton btnNewButton_1 = new JButton("ص");
	private final JButton btnNewButton_2 = new JButton("ق");
	private final JButton btnNewButton_3 = new JButton("ف");
	private final JButton btnNewButton_4 = new JButton("غ");
	private final JButton btnNewButton_5 = new JButton("ع");
	private final JButton btnNewButton_6 = new JButton("ه");
	private final JButton btnNewButton_7 = new JButton("خ");
	private final JButton btnNewButton_8 = new JButton("ح");
	private final JButton btnNewButton_9 = new JButton("ج");
	private final JButton button = new JButton("ش");
	private final JButton button_1 = new JButton("س");
	private final JButton button_2 = new JButton("ي");
	private final JButton button_5 = new JButton("ا");
	private final JButton button_6 = new JButton("ت");
	private final JButton button_7 = new JButton("ن");
	private final JButton button_8 = new JButton("م");
	private final JButton button_9 = new JButton("ك");
	private final JPanel panel_2 = new JPanel();
	private final JButton button_10 = new JButton("ظ");
	private final JButton button_11 = new JButton("ط");
	private final JButton button_12 = new JButton("ذ");
	private final JButton button_13 = new JButton("د");
	private final JButton button_14 = new JButton("ز");
	private final JButton button_15 = new JButton("ر");
	private final JButton button_16 = new JButton("و");
	private final JButton button_17 = new JButton("ة");
	private final JButton button_18 = new JButton("ث");
	private final JPanel panel_5 = new JPanel();
	private final JButton button_23 = new JButton("ب");
	private final JButton button_26 = new JButton("ل");
	private final JButton button_30 = new JButton("ء");
	private final JPanel arabicPanel = new JPanel();
	private final JTextArea textArea = new JTextArea();
	private final JScrollPane scrollPane = new JScrollPane();
	private final JPopupMenu popupMenu_Input = new JPopupMenu();

	public ScratchPanel()
	{
		super();
		initComponents();
		initPopupMenu();
		initListeners();
	}

	private void initComponents()
	{
		setLayout(new BorderLayout());
		panel_5.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
		panel_5.add(button);
		button.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_1);
		button_1.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_2);
		button_2.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		button_23
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_23);
		button_26
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_5.add(button_26);
		panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
		button_10
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_10);
		button_11
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_11);
		button_12
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_12);
		button_13
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_13);
		button_14
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_14);
		button_15
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_15);
		button_16
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_16);
		button_17
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_17);
		button_18
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		panel_2.add(button_18);

		arabicPanel.setPreferredSize(new Dimension(556, 111));
		GroupLayout groupLayout = new GroupLayout(arabicPanel);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																panel,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																556,
																Short.MAX_VALUE)
														.addComponent(
																panel_5,
																GroupLayout.DEFAULT_SIZE,
																556,
																Short.MAX_VALUE)
														.addComponent(
																panel_2,
																Alignment.LEADING,
																GroupLayout.DEFAULT_SIZE,
																556,
																Short.MAX_VALUE))
										.addGap(0)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 31,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_5, GroupLayout.PREFERRED_SIZE, 31,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 31,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(77, Short.MAX_VALUE)));
		panel_5.add(button_30);
		button_30
				.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_5);
		button_5.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_6);
		button_6.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_7);
		button_7.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_8);
		button_8.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));
		panel_5.add(button_9);
		button_9.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN, 16));

		btnNewButton.setFont(FontLibrary.get(FontLibrary.CODE2000, Font.PLAIN,
				16));
		btnNewButton_1.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_2.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_3.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_4.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_5.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_6.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_7.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_8.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		btnNewButton_9.setFont(FontLibrary.get(FontLibrary.CODE2000,
				Font.PLAIN, 16));
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));

		panel.add(btnNewButton);

		panel.add(btnNewButton_1);

		panel.add(btnNewButton_2);

		panel.add(btnNewButton_3);

		panel.add(btnNewButton_4);

		panel.add(btnNewButton_5);

		panel.add(btnNewButton_6);

		panel.add(btnNewButton_7);

		panel.add(btnNewButton_8);

		panel.add(btnNewButton_9);

		arabicPanel.setLayout(groupLayout);

		scrollPane.setViewportView(textArea);

		add(scrollPane, BorderLayout.CENTER);

		setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createMatteBorder(3, 3, 3, 3, Color.DARK_GRAY),
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"ScratchPad"), 0, TitledBorder.BELOW_TOP, FontLibrary
						.get(FontLibrary.DROID_SANS, Font.BOLD, 12),
				Color.DARK_GRAY));

		setPreferredSize(new Dimension(560, 300));
	}

	private void initPopupMenu()
	{
		JMenuItem item;

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "SendToSearch"));
		item.setMargin(UIConstants.MARGIN);
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				MainFrame.view_Windows.mountSearchContainer();
				MainFrame.view_Windows.getSearchPanel().setQuery(
						textArea.getSelectedText());
			}
		});
		popupMenu_Input.add(item);
		popupMenu_Input.addSeparator();

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "Paste"));
		item.setMargin(UIConstants.MARGIN);
		item.setAccelerator(KeyStroke.getKeyStroke("ctrl V"));
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				String s = GUIToolkit.getClipboardContents();
				if (s != null && s.length() > 0)
				{
					send(s);
				}
			}
		});
		popupMenu_Input.add(item);
		popupMenu_Input.addSeparator();

		item = new JMenuItem(ProjectLocalization.getString(
				ProjectConstants.CORE_PROJECT, "SelectAll"));
		item.setMargin(UIConstants.MARGIN);
		item.setAccelerator(KeyStroke.getKeyStroke("ctrl A"));
		item.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				textArea.selectAll();
			}
		});
		popupMenu_Input.add(item);
		popupMenu_Input.addSeparator();

		final JCheckBoxMenuItem item2;
		item2 = new JCheckBoxMenuItem(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT, "ShowArabicKeys"));
		item2.setMargin(UIConstants.MARGIN);
		item2.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (item2.isSelected())
				{
					add(arabicPanel, BorderLayout.SOUTH);
					revalidate();
				}
				else
				{
					remove(arabicPanel);
					revalidate();
				}
			}
		});
		popupMenu_Input.add(item2);
	}

	private void refreshInputPopup()
	{
		String s = GUIToolkit.getClipboardContents();
		if (s != null && s.length() > 0) popupMenu_Input.getComponent(2)
				.setEnabled(true);
		else popupMenu_Input.getComponent(2).setEnabled(false);
		if (textArea.getText() != null && !textArea.getText().isEmpty()) popupMenu_Input
				.getComponent(4).setEnabled(true);
		else popupMenu_Input.getComponent(4).setEnabled(false);
		if (textArea.getSelectedText() != null
				&& !textArea.getSelectedText().isEmpty()) popupMenu_Input
				.getComponent(0).setEnabled(true);
		else popupMenu_Input.getComponent(0).setEnabled(false);
	}

	private void initListeners()
	{
		textArea.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mousePressed(MouseEvent e)
			{
				if (e.isPopupTrigger())
				{
					refreshInputPopup();
					popupMenu_Input.show(e.getComponent(), e.getX(), e.getY());
				}
			}

			@Override
			public void mouseReleased(MouseEvent e)
			{
				if (e.isPopupTrigger())
				{
					refreshInputPopup();
					popupMenu_Input.show(e.getComponent(), e.getX(), e.getY());
				}
			}
		});

		btnNewButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton.getText());
			}
		});

		btnNewButton_1.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_1.getText());
			}
		});

		btnNewButton_2.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_2.getText());
			}
		});

		btnNewButton_3.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_3.getText());
			}
		});

		btnNewButton_4.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_4.getText());
			}
		});

		btnNewButton_5.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_5.getText());
			}
		});

		btnNewButton_6.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_6.getText());
			}
		});

		btnNewButton_7.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_7.getText());
			}
		});

		btnNewButton_8.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_8.getText());
			}
		});

		btnNewButton_9.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(btnNewButton_9.getText());
			}
		});

		button.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button.getText());
			}
		});

		button_1.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_1.getText());
			}
		});

		button_2.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_2.getText());
			}
		});

		button_5.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_5.getText());
			}
		});

		button_6.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_6.getText());
			}
		});

		button_7.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_7.getText());
			}
		});

		button_8.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_8.getText());
			}
		});

		button_9.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_9.getText());
			}
		});

		button_10.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_10.getText());
			}
		});

		button_11.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_11.getText());
			}
		});

		button_12.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_12.getText());
			}
		});

		button_13.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_13.getText());
			}
		});

		button_14.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_14.getText());
			}
		});

		button_15.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_15.getText());
			}
		});

		button_16.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_16.getText());
			}
		});

		button_17.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_17.getText());
			}
		});

		button_18.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_18.getText());
			}
		});

		button_23.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_23.getText());
			}
		});

		button_26.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_26.getText());
			}
		});

		button_30.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				send(button_30.getText());
			}
		});
	}

	@Override
	public void requestFocus()
	{
		textArea.requestFocus();
	}

	@Override
	public boolean hasFocus()
	{
		return textArea.hasFocus();
	}

	public String getText()
	{
		return textArea.getText();
	}

	public void setText(String s)
	{
		textArea.setText(s);
	}

	private void send(String s)
	{
		textArea.setText(textArea.getText() + s);
		textArea.requestFocus();
	}

}
